package com.first.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.TbLink;
import com.first.common.vo.Result;
import com.first.system.dao.LinkDao;
import com.first.system.service.LinkService;
import com.first.system.vo.LinkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @program: first-project
 * @description:
 * @author: 闫锦超
 * @create: 2023-05-27 14:03
 **/
@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkDao linkDao;

    @Override
    public Result getAllLink(Integer currentPage, Integer pageSize, LinkVo linkVo) {
        Page<TbLink> page = new Page(currentPage, pageSize);
        QueryWrapper<TbLink> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);
        if (StringUtils.hasText(linkVo.getLinkName())) {
            wrapper.like("link_name", linkVo.getLinkName());
        }
        if (linkVo.getIsDisable() != "" && linkVo.getIsDisable() != null) {
            wrapper.eq("is_disable", linkVo.getIsDisable());
        }
        Page<TbLink> tbHeadNavPage = linkDao.selectPage(page, wrapper);
        return Result.success(tbHeadNavPage);
    }

    @Override
    public Result addLink(TbLink tbLink) {
        if (StringUtils.hasText(tbLink.getLinkId())) {
            linkDao.updateById(tbLink);
            return Result.success();
        }
        TbLink tbLink1 = new TbLink(IdUtil.getSnowflakeNextId() + "", tbLink.getLinkUrl(), tbLink.getLinkName(), 0, 0, LocalDateTime.now(), LocalDateTime.now());
        int insert = linkDao.insert(tbLink1);
        if (insert != 0) {
            return Result.success();
        }
        return null;
    }

    @Override
    public Result changeStatus(String linkId, Integer isDisable) {
        TbLink tbLink = new TbLink();
        tbLink.setLinkId(linkId);
        tbLink.setIsDisable(isDisable);
        int i = linkDao.updateById(tbLink);
        if(i!=0){
            return Result.success();
        }
        return null;
    }

    @Override
    public Result deleteLink(TbLink tbLink) {
        tbLink.setIsDeleted(1);
        int i = linkDao.updateById(tbLink);
        if(i!=0){
            return Result.success();
        }
        return null;
    }

}
